package com.wwwarehouse.dubbo.client;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.wwwarehouse.commons.dubbo.DubboContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class ClientTestController {

    @Autowired
    MyGenericService myGenericService;

    @PostMapping("/test")
    public void test(@RequestBody Map map) {
        String interfaceName = (String) map.get("interface");
        String method = (String) map.get("method");
        Map data = (Map) map.get("data");
        log.info("{}, {}, {}", interfaceName, method, data);
        DubboContextUtil.getContextUtil().setAttachment("traceId", "11111111");
        myGenericService.invoke(interfaceName, method, data.values().toArray());
    }
    @PostMapping("/test2")
    public void test2(@RequestBody JSONObject jsonObject) {
        String interfaceName = (String) jsonObject.get("interface");
        String method = (String) jsonObject.get("method");
        JSONObject data = jsonObject.getJSONObject("data");
        log.info("{}, {}, {}", interfaceName, method, data);
        DubboContextUtil.getContextUtil().setAttachment("traceId", "222222");

        Object[] array = data.values().toArray();
        List<String> list = Arrays.stream(array).map(value -> value.toString()).collect(Collectors.toList());
        myGenericService.invoke(interfaceName, method, data.values().toArray());
    }
}
